package com.example.listener;

import cn.hutool.core.date.DateUtil;
import com.example.dto.UserDto;
import com.example.event.MyEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;

import javax.servlet.annotation.WebListener;

/**
 * Created by IntelliJ IDEA2021.3
 *
 * @Author: Tenghw
 * @Date: 2022/09/25  20:26
 * @Description:
 */
@WebListener(value = "监听事件类")
@Configuration
@Slf4j
public class MyListener implements ApplicationListener<MyEvent> {
    @Override
    public void onApplicationEvent(MyEvent event) {
        Object source = event.getSource();
        UserDto userDto = (UserDto) source;
        //当前时间字符串，格式：yyyy-MM-dd HH:mm:ss
        String now = DateUtil.now();
        log.warn("当前时间：{}，这是事件被监听器监听的事件类:{}", now, userDto);
        //比如业务时增加积分的，把userDto存入数据库
        log.warn("入库的数据为：{}", userDto);
    }
}

//@Slf4j
//@Component
//public class MyListener {
//    @EventListener
//    public void onApplicationEvent(Object event) {
//
//        //当前时间字符串，格式：yyyy-MM-dd HH:mm:ss
//        String now = DateUtil.now();
//        log.warn("当前时间：{}，这是事件被监听器监听的事件类:{}", now, event.getClass());
//        //比如业务时增加积分的，把userDto存入数据库
//
//    }
//}

